package com.younion.action.manage;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.IActiveService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.SysConstants;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.vo.ActiveVO;
import com.younion.vo.UserVO;

import cpcns.jhsks.util.MapCollectionUtils;

public class ActiveAction extends BaseAction {
	
	private static final long serialVersionUID = -6442022874913706223L;

	protected static final Log logger = LogFactory.getLog(ActiveAction.class);
	
	private IActiveService activeService = null;
	
	private ActiveVO activeVO = new ActiveVO();
	
	private List<ActiveVO> activeList;
	
	private String ids;
	
	private String searchType;
	
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listActive")
	public String listActive() {
		try {
			Map paramMap = null;
			if(getSessionUser().getCustomer() != null){
				activeVO.setCustomerId(getSessionUser().getCustomer().getId());
			}
			if (this.activeVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(this.activeVO));
			}
			if (this.getSessionUser().getCustomer() != null)
				paramMap.put("customerId", this.getSessionUser().getCustomer().getId());
			// 获取总记录数
			int total = this.activeService.selectCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			this.activeList = this.activeService.selectForPage(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(2, "活动列表", "查询活动列表");
		return SUCCESS;
	}
	
	/**
	 * 新增
	 */
	@RequiresPermissions("addActive")
	public String addActive() {
		return SUCCESS;
	}
	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveActive")
	public String saveActive() {
		UserVO user = this.getSessionUser();
		try {
			if (this.activeVO.getTop() == 1) {
				this.activeVO.setTop(this.activeService.selectMaxTop() + 1);
			} else if (this.activeVO.getId() != null) {
				this.activeVO.setTop(null);
			}
			if (this.activeVO.getId() == null) {
				this.activeVO.setUserId(user.getId());
				this.activeVO.setId(this.activeService.insertByVo(this.activeVO));
				this.addLog(3, "添加活动："+this.activeVO.getTitle(), "ID为"+this.activeVO.getId());
			} else {
				Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(this.activeVO));
				this.activeService.updateActiveByMap(paramMap);
				this.addLog(4, "修改活动："+this.activeVO.getTitle(), "ID为"+this.activeVO.getId());
			}
			ImageFileUtil.processImageFiles(this.activeVO.getId(), "1", request);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		request.setAttribute("refreshNavTabId", "activeList");
		return SUCCESS;
	}
	/**
	 *修改保存，调度Jsp
	 */
	@RequiresPermissions("editActive")
	public String editActive() {
		try {
			this.activeVO = this.activeService.selectActiveById(this.activeVO.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("deleteActive")
	public String deleteActive() {
		String realPath = request.getSession().getServletContext().getRealPath(SysConstants.ACTIVE_IMG_PATH);
		File file = new File(realPath);
		try {
			ActiveVO vo = this.activeService.selectActiveById(this.activeVO.getId());
			this.activeService.deleteActiveById(this.activeVO.getId());
			File toFile = new File(file, String.valueOf(this.activeVO.getId()));
			ImageFileUtil.deleteImage(toFile, vo.getPicName(), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "删除活动："+this.activeVO.getTitle(), "ID为"+this.activeVO.getId());
		request.setAttribute("refreshNavTabId", "activeList");
		return SUCCESS;
	}
	/**
	 * 批量删除
	 */
	@RequiresPermissions("deleteActives")
	public String deleteActives() {
		String realPath = request.getSession().getServletContext().getRealPath(SysConstants.ACTIVE_IMG_PATH);
		List list = Arrays.asList(this.ids.split(","));
		File file = new File(realPath);
		try {
			List<ActiveVO> listVo = this.activeService.selectByIds(list);
			for (ActiveVO vo : listVo) {
				File toFile = new File(file, String.valueOf(vo.getId()));
				ImageFileUtil.deleteImage(toFile, vo.getPicName(), true);
			}
			this.activeService.deleteActiveByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "批量删除活动", "ID为"+this.ids);
		request.setAttribute("refreshNavTabId", "activeList");
		return SUCCESS;
	}
	
	@RequiresPermissions("updateTopActive")
	public String updateTopActive() {
		try {
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("id", this.activeVO.getId());
			Integer maxTop = this.activeService.selectMaxTop();
			map.put("maxTop", maxTop + 1);
			this.activeService.updateTopById(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(4, "置顶活动："+this.activeVO.getTitle(), "ID为"+this.activeVO.getId());
		request.setAttribute("refreshNavTabId", "activeList");
		return SUCCESS;
	}
	
	public void setActiveService(IActiveService activeService) {
		this.activeService = activeService;
	}
	
	public List<ActiveVO> getActiveList() {
		return this.activeList;
	}

	public ActiveVO getActiveVO() {
		return activeVO;
	}

	public void setActiveVO(ActiveVO activeVO) {
		this.activeVO = activeVO;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

}
